<?php
namespace app\admin\controller;
use think\Request;
use think\Controller;
use think\Db;
use app\admin\controller\Base;
use think\Model;
use think\file;
use app\admin\model\Category;
header("content-type:text/html;charset=utf-8");
class Goods extends Base
{  
    // 商品列表页面
    public function goods()
    {

        $where=[];
       
         $cat_id=input('get.cat');
        if(!empty($cat_id))
                
        {
            $where['g.cat_id']=$cat_id;
        }

        $brand_id=input('get.brand');
        if(!empty($brand_id))
        {
            $where['g.brand_id']=$brand_id;
        }

        $provider_id=input('get.provider');
        if(!empty($provider_id))
        {
            $where['g.provider_id']=$provider_id;
        }

        $con=input('get.con');
        if(!empty($con))
        {
            $where['g.goods_name']=array('like',"%$con%");
        }
//        var_dump($keyword);
        $page=input('get.page',1);
//        var_dump($page);
        $limit=5;
        $off=($page-1)*$limit;

        $top=$page-1<1?1:$page-1;

        $list=Db::name('goods')->select();
        $num=count($list);
        $last=ceil($num/$limit);
        $down=$page+1>$last?$last:$page+1;

        $arr=[ 'top'=>$top, 'down'=>$down, 'page'=>$page,'last'=>$last ,'num'=>$num,'limit'=>$limit];
        // 查询商品表
        $list = Db::name('goods')->where($where)
         ->alias('g')
         // ->join('category c','g.cat_id = c.cat_id')
         ->join('brand b','g.brand_id = b.brand_id')
         // ->join('provider p','g.provider_id=p.provider_id')
         ->limit($off,$limit)->select();

       //  查询品牌
        $brand_list=Db::name('brand')->select();
        // 查询商品类型
        $category=new Category();
        $category_data=Db::name('category')->select();
        $cate_list=$category->createTree($category_data);
// 查询供货商的信息
         $provider =Db::table('provider')
            ->alias('g')
            ->join('provider p','g.provider_id = p.provider_id','LEFT')
            ->select(); 
        
        $arr=['arr'=>$arr,'list'=>$list];
//        var_dump($arr);die;
        $this->assign('count',count($list));
        if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
        {
            echo json_encode($arr);
        }else{

            $this->assign('provider',$provider);
            $this->assign('cate_list',$cate_list);
            $this->assign('brand_list',$brand_list);
            $this->assign('arr',$arr);
            return $this->fetch();
        }

    }
    public function change_sale()
    {

            $id=input('goods_id');
            $data=input('ls_on_sale');
            $field='ls_on_sale';
            $this->c($id,$data,$field);
            $id=input('get.goods_id');
            $data=input('get.Is_on_sale');
            $field='Is_on_sale';
            $resulta = Db::name('goods')
            ->where('goods_id', $id)
            ->update([$field => $data]);
            echo json_encode($resulta);    
    }

    public function change_best()
    {
        $id=input('get.goods_id');
        $data=input('get.is_best');
        $field='is_best';
        // $this->c($id,$data,$field); 
         $resulta = Db::name('goods')
            ->where('goods_id', $id)
            ->update([$field => $data]);
        echo json_encode($resulta);
    }

    public function change_new()
    {

        $id=input('goods_id');
        $data=input('is_new');
        $field='is_new';
        // $this->c($id,$data,$field);
         $resulta = Db::name('goods')
            ->where('goods_id', $id)
            ->update([$field => $data]);
        echo json_encode($resulta);
    }

    public function change_hot()
    {

        $id=input('goods_id');
        $data=input('is_hot');
        $field='is_hot';
        // $this->c($id,$data,$field);
         $resulta = Db::name('goods')
            ->where('goods_id', $id)
            ->update([$field => $data]);
        echo json_encode($resulta);
    }

    // public function c($id,$data,$field)
    // {
    //     $resulta = Db::name('goods')
    //         ->where('goods_id', $id)
    //         ->update([$field => $data]);
    //     echo json_encode($resulta);
    // }

// 商品添加页面
    public function addgoods()
    { 
      // 查询商品品牌
        $brand_list=Db::name('brand')->select();
       
        // 查询商品分类
        $cate=new Category();
        $category=Db::name('category')->select();
        $cate_list=$cate->createTree($category);
       // 查询供货商
         $provider =Db::table('provider')
            ->alias('g')
            ->join('provider p','g.provider_id = p.provider_id','LEFT')
            ->select(); 
            // 查询商品类型
            $goods_type=Db::name('goods_type')->select();

        $this->assign('goods_type',$goods_type);  
        $this->assign('provider',$provider);  
        $this->assign('brand_list',$brand_list);
        $this->assign('cate_list',$cate_list);
        return $this->fetch();
    }
    // 添加商品
     public function add_goods()
    {
            $data=input('post.');
            //var_dump($data);die;
            if(empty($data['goods_sn']))
            {
                $data['goods_sn']=$this->createSn();
            }
            if(empty($data['cat_id']))
            {
                 // $this->success('商品分类必须填写');die;
                 $this->error('商品分类必须填写','goods/addgoods');
            }
            if(empty($data['brand_id']))
            {
                // $this->success('商品品牌必须填写');die;
                 $this->error('商品品牌必须填写','goods/addgoods');
            }
            if(empty($data['provider_id']))
            {
                 $this->error('供货商必须填写','goods/addgoods');
            }


/*              if(empty($data['goods_img']))
            {
                 $this->error('图片必须选择','goods/addgoods');
            }*/
          

      
        $file=request()->file('goods_img');
        if(empty($file))
        {
             $this->error('图片必须选择','goods/addgoods');
        }

        $info=$file->move(ROOT_PATH.'public'.DS.'uploads');
     
        $filename=$info->getSaveName();
        $data['goods_img']=$filename;

        $result=Db('goods')->insert($data);
        if($result)
        {
            $this->success('添加商品成功','goods/goods');
        }
        else
        {
            $this->error('添加商品失败','goods/addgoods');
        }
      
    }
// 随机生成货号
    public function createSn()
    {
        return 'oneNB'.time().rand(1000,9999);
    }
// 删除商品
      public function del()
    {
        $goods_id=input('get.goods_id');

        $del = Db::execute("DELETE goods,`comment` FROM goods,`comment` WHERE goods.goods_id = '$goods_id' and goods.goods_id = `comment`.goods_id");
        // $del=Db::name('goods')->where(['goods_id'=>$goods_id])->delete();
        // var_dump($del);die;
        if($del)
        {
            $this->success('删除商品成功','goods/goods');
        }else{
            $this->error('删除商品失败','goods/goods');
        }
       
    }
    


}